package com.jdragon.tljrobot.client.utils.common;

import com.jdragon.tljrobot.client.entry.Article;
import lombok.NonNull;

import java.util.Arrays;
import java.util.stream.Collectors;

public class ArticleRegex {
    public static Article regexStringToArticle(@NonNull String articleStr) {
        String[] lines = articleStr.split("[\n\r]");
        if (lines.length > 2) {
            String title = lines[0];
            String lastLine = lines[lines.length - 1];
            String content = Arrays.stream(lines).skip(1).limit(lines.length - 2).collect(Collectors.joining());
            lastLine = lastLine.replaceAll("-+第(\\d+)段.*", "$1");

            if (lastLine.matches("\\d+")) {
                return Article.getArticleSingleton(Integer.parseInt(lastLine), title, content);
            }
        }

        return Article.getArticleSingleton(1, "未知标题", articleStr);
    }
}
